package com.example.administrator.myapplication.bottom_menu;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.example.administrator.myapplication.Fragment1;
import com.example.administrator.myapplication.Fragment2;
import com.example.administrator.myapplication.Fragment3;
import com.example.administrator.myapplication.Fragment4;
import com.example.administrator.myapplication.MyFragmentPageAdapter;
import com.example.administrator.myapplication.R;

import java.util.ArrayList;
import java.util.List;

public class BottomMenuActivity extends AppCompatActivity {
    //参考网址 http://www.tuicool.com/articles/RzeUZf
    private ViewPager mViewPager;
    private TabLayout mTabLayout;

    private MyFragmentPageAdapter mAdapter;
    private  List<Fragment> mFragments;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bottom_menu);
        init();
    }

    private void init() {
        //初始化控件
        mViewPager = (ViewPager) findViewById(R.id.view_pager_tab);
        mTabLayout = (TabLayout) findViewById(R.id.tab_layout);
        //将布局加入List
        mFragments = new ArrayList<>();
        mFragments.add(new Fragment1());
        mFragments.add(new Fragment2());
        mFragments.add(new Fragment3());
        mFragments.add(new Fragment4());
        mAdapter = new MyFragmentPageAdapter(getSupportFragmentManager(),mFragments,this);

        //关联适配器
        mViewPager.setAdapter(mAdapter);

        //TabLayout跟ViewPager关联起来
        mTabLayout.setupWithViewPager(mViewPager);

        //为TabLayout的Item赋值
        for (int i = 0; i <mTabLayout.getTabCount() ; i++) {
            TabLayout.Tab tab = mTabLayout.getTabAt(i);
            if(tab!=null){
                tab.setCustomView(mAdapter.getTabView(i));
            }
        }
    }

}
